<template>
  <div class="view-fill">
    <div v-if="!showModify.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        title="资产字典详情"
        :btn-list="btnList"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="returnBack"
      >
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information
              :info="formInfo"
              :image-field="formInfo.imageField"
            >
            </basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>证照信息</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="licenseInfo"></basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="'维保商'" />
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <el-form :model="tableInfo" ref="tableForm" style="width: 100%">
                <page-table
                  :refresh="tableInfo.refresh"
                  :init-curpage="tableInfo.initCurpage"
                  v-model:data="tableInfo.data"
                  :pager="false"
                  :check-box="false"
                  :tabIndex="true"
                  :api="tableInfo.api"
                  :query="filterInfo.query"
                  :field-list="tableInfo.fieldList"
                  :handle="tableInfo.handle"
                  :isDynamic="true"
                  @handleEvent="handleEvent"
                />
              </el-form>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <!-- 新增或修改 -->
    <equipment-archives-modify
      v-if="showModify.visible"
      :id="id"
      v-model:visible="showModify.visible"
      :title="showModify.title"
      :type="showModify.type"
      @returnModify="modifyCallback"
    />
  </div>
</template>

<script setup name="assetDictDetails">
import utils from '@/common/js/utils' // 公共方法
import { orderAffairAll } from '@/api/affair'
import EquipmentArchivesModify from './modify'
import { detailEquipmentArchives } from '@/api/equipment'
import {
  getMaintenanceProductAll,
  setMaintenanceProductDefault,
} from '@/api/core'
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  id: {
    type: [String, Number],
  },
})
const formInfo = reactive({
  ref: null,
  data: {},
  imageField: {
    label: '资产字典照片',
    value: 'pictureList',
    btnDisabled: false,
  },
  fieldList: [
    {
      label: '资产字典名称',
      value: 'commonName',
      type: 'text',
    },
    { label: '资产字典编码', value: 'code', type: 'text' },
    {
      label: '状态',
      value: 'statusValue',
      type: 'text',
    },
    {
      label: '医疗器械分类目录',
      value: 'deviceTypeValue',
      type: 'text',
    },
    {
      label: '计量单位',
      value: 'unitName',
      type: 'text',
    },
    {
      label: '品牌',
      value: 'trademark',
      type: 'text',
    },
    {
      label: '规格',
      value: 'standard',
      type: 'text',
    },
    {
      label: '资产分类',
      value: 'assetTypeValue',
      type: 'text',
    },
    {
      label: '资产财务分类',
      value: 'financeTypeValue',
      type: 'text',
    },
    {
      label: '折旧方法',
      value: 'deprMethodTypeValue',
      type: 'text',
    },
    {
      label: '折旧惯例',
      value: 'deprConventionValue',
      type: 'text',
      show: true,
    },
    {
      label: '折旧月限',
      value: 'deprMonthsLimitValue',
      type: 'text',
      show: true,
    },

    {
      label: '强制收货验收',
      value: 'forceAcceptValue',
      type: 'text',
    },
    // {
    //   label: '是否入库管理',
    //   value: 'stockManageValue',
    //   type: 'text',
    // },
    {
      label: '设备标识',
      value: 'assetMarkValue',
      type: 'text',
    },
    {
      label: '是否计量资产',
      value: 'isMeteringName',
      type: 'text',
    },
    {
      label: '是否维保',
      value: 'isMaintenanceName',
      type: 'text',
    },
    {
      label: '是否医疗设备',
      value: 'isMedicalEquipmentValue',
      type: 'text',
    },
    {
      label: '报修预警时长(分钟)',
      value: 'repairWarnTime',
      type: 'text',
    },
    {
      label: '备注',
      value: 'remark',
      type: 'text',
    },
  ],
  labelWidth: '120px',
})
const licenseInfo = reactive({
  ref: null,
  data: {},
  fieldList: [
    {
      label: '是否关联证照',
      value: 'needLicenseValue',
      type: 'text',
    },
    {
      label: '证号',
      value: 'licenseNumber',
      type: 'text',
    },
    {
      label: '证照名称',
      value: 'licenseName',
      type: 'text',
    },
    {
      label: '英文名称',
      value: 'enName',
      type: 'text',
    },
    {
      label: '证照生效日期',
      value: 'licenseBeginDate',
      type: 'text',
    },
    {
      label: '证照失效日期',
      value: 'licenseEndDate',
      type: 'text',
    },
    {
      label: '生产厂家',
      value: 'brandName',
      type: 'text',
    },
  ],
})
// 过滤相关配置
const filterInfo = reactive({
  query: {
    productId: props.id,
    sort: 'is_default desc,create_time desc',
  },
  list: [],
})
// 操作流水
const affairTableInfo = reactive({
  query: {
    orderType: 46,
    orderId: props.id,
    sort: 'operate_time desc',
  },
})
const btnList = reactive([
  {
    label: '修改',
    event: update,
    show: proxy.$permission('assetDict:update'),
  },
])
const showModify = reactive({
  title: '修改',
  type: 'update',
  visible: false,
})
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  api: getMaintenanceProductAll,
  data: [],
  selections: [],
  fieldList: [
    { label: '维保商', value: 'maintenanceName' },
    { label: '机构代码', value: 'maintenanceCode' },
    { label: '联系人', value: 'contact' },
    { label: '联系电话', value: 'contactNumber' },
    { label: '是否默认', value: 'isDefaultValue' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '设为默认',
        event: setDefault,
        show: true,
        ifRender: function (row) {
          return row.isDefault !== 1
        },
      },
    ],
  },
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})
watch(
  () => props.visible,
  (newVal) => {
    showDialog.value = newVal
  },
  {
    immediate: true,
  }
)
onMounted(() => {
  getList()
  getDeatil()
})
function showFieldListItem(show, nameValue) {
  formInfo.fieldList.forEach((item) => {
    if (nameValue === item.value) {
      item.show = show
    }
  })
}
function returnBack() {
  emits('returnBack')
}
function getDeatil() {
  detailEquipmentArchives(props.id).then((resp) => {
    if (resp && resp.success) {
      let data = resp.data
      formInfo.data = {
        commonName: data.commonName,
        code: data.code,
        unitId: data.unitId,
        unitName: data.unitName,
        standard: data.standard,
        status: data.status,
        statusValue: data.statusValue,
        remark: data.remark,
        trademark: data.trademark,
        financeType: data.financeType,
        financeTypeValue: data.financeTypeValue,
        forceAccept: data.forceAccept,
        forceAcceptValue: data.forceAcceptValue,
        // stockManage: data.stockManage,
        // stockManageValue: data.stockManageValue,
        picture: data.picture,
        deviceType: data.deviceType,
        deviceTypeValue: data.deviceTypeValue,
        createUser: data.createUser,
        createTime: data.createTime,
        orgId: data.orgId,
        assetType: data.assetType,
        assetTypeValue: data.assetTypeValue,
        deprConvention: data.deprConvention,
        deprConventionValue: data.deprConventionValue,
        deprMethodType: data.deprMethodType,
        deprMethodTypeValue: data.deprMethodTypeValue,
        deprMonthsLimitValue: data.deprMonthsLimitValue,
        deprYearsLimit: data.deprYearsLimit,
        assetMarkValue: data.assetMarkValue,
        isMaintenanceName: data.isMaintenanceName,
        isMeteringName: data.isMeteringName,
        isMedicalEquipmentValue: data.isMedicalEquipmentValue,
        repairWarnTime: data.repairWarnTime + '分',
      }
      licenseInfo.data = {
        needLicense: data.needLicense,
        needLicenseValue: data.needLicenseValue,
        licenseNumber: data.licenseNumber,
        licenseTypeValue: data.licenseTypeValue,
        licenseName: data.licenseName,
        licenseBeginDate: data.licenseBeginDate,
        licenseEndDate: data.licenseEndDate,
        brandName: data.brandName,
        enName: data.enName,
        licenseId: data.licenseId,
        brandId: data.brand,
      }
      if (formInfo.data.picture) {
        formInfo.data.pictureList = utils.splitArr(formInfo.data.picture)
      }
      if (formInfo.data.deprMethodType == '0') {
        //如果不折旧，那么隐藏所有跟折旧有关的值
        showFieldListItem(false, 'deprConventionValue')
        showFieldListItem(false, 'deprMonthsLimit')
      } else {
        showFieldListItem(true, 'deprConventionValue')
        showFieldListItem(true, 'deprMonthsLimit')
      }
    }
  })
}
//设置默认
function setDefault(row) {
  setMaintenanceProductDefault(row.id).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function getList() {
  tableInfo.refresh = Math.random()
}

// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.selections = [...data]
      break
  }
}

function update() {
  showModify.visible = true
}
function modifyCallback() {
  getList()
  getDeatil()
}
</script>

<style scoped></style>
